home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Applications / TimGA 1.2.1 / .h / CPopulation.h < prev    next >
Encoding:
Text File  |  1997-07-16  |  5.0 KB  |  150 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //    CPopulation.h        ©1995-97 Timo Eloranta        All rights reserved.
  3. // ===========================================================================
  4. //    CPopulation.cp        (press Command-Tab to open the associated source file)
  5.  
  6. #pragma once
  7.  
  8. #include <vector.h>            // MSL
  9. #include <iterator.h>        // MSL
  10.  
  11. #include "MyStructs.h"
  12. #include "CGraphDrawing.h"
  13.  
  14. typedef vector<CGraphPtr, allocator<CGraphPtr> >    GraphPtrVector;
  15. typedef vector<UInt16,    allocator<UInt16> >        UInt16Vector;
  16. typedef vector<SNodePair, allocator<SNodePair> >    SNodePairVector;
  17.  
  18. class LAnimateCursor;
  19.  
  20. class CPopulation
  21. {
  22.     private:
  23.                                 // === These are set before running ===
  24.         Int16    mPopSize,            // Population size
  25.                 mMaxRuns,            // Maximum nbr of test runs
  26.                 mNodeCount,            // Nbr of nodes in the graph
  27.                 mEdgeCount,            // Nbr of edges in the graph
  28.                 mGridSize,            // The size (N) of the grid (NxN)
  29.                 mCrossoverProb,        // Crossover probability [0...100]
  30.                 mMutationProb;        // Mutation probability [0...100]
  31.         Boolean    mTermCondExists;    // Is there a termination condition?
  32.  
  33.         SSelection        mSelection;        // Selection variables (see MyStructs.h)
  34.         STermination    mTermination;    // Termination condition
  35.         
  36.                                 // === Runtime variables ===
  37.         Int32    mCurGeneration,        // Nbr of current generation
  38.                 mBestGeneration,    // Nbr of the generation when we found
  39.                                     // the current best ever chromosome
  40.                 mLeastCrossings,    // Smallest nbr of crossings in this gener.
  41.                 mAvgCrossings,        // Avg. nbr of crossings in this gener.
  42.                 mRunningTime,        // Running time in ticks (1s = 60ticks)
  43.                 mBestTime;            // Time (tickcount) when we found
  44.                                     // the current best ever chromosome
  45.                 
  46.         GraphPtrVector        mDrawings;        // A list of chromosomes
  47.         SGraphPtrComparator    mComparator;    // A comparator for this list
  48.         
  49.         UInt16Vector        mSelectionArray;    // An array used in the
  50.                                                 // selection process
  51.         
  52.         CGraphDrawing        mBestEver;        // The fittest chromosome 
  53.                                             // we've ever had...
  54.         CGraphDrawing        mCrossTemp1,    // A pair of chromosomes which
  55.                             mCrossTemp2;    // are used in crossover phase
  56.                                             
  57.                                 // === Private methods ===
  58.         void        AddCrossMemories();
  59.         void        SetUpEdges( SNodePairVector    *inEdgeArray );
  60.         void        InitBestEver();
  61.         void        InitSelectionArray();
  62.         Boolean        DoOneIteration();
  63.         void        DoSelection();
  64.         Int16        SelectOne();
  65.         void        DoMutate();
  66.         void        DoCrossover();
  67.         void        DoRectCrossover (    CGraphPtr inCrosser1,
  68.                                         CGraphPtr inCrosser2 );
  69.  
  70.         Boolean        TerminationCondition();
  71.         
  72.     public:
  73.                                 // === Public methods ===
  74.                         CPopulation() {};
  75.                         ~CPopulation();
  76.                         
  77.         void            JunkGraphs();
  78.         
  79.         void            Initialize(    Boolean                inFirstTime,
  80.                                     SGraphSize            *inGraphSize = nil,
  81.                                     SNodePairVector        *inEdgeArray = nil );
  82.  
  83.                                     // Returns true if new best was found
  84.         Boolean            Evaluate( LAnimateCursor *inSpinCursor = nil );        
  85.         
  86.         void            DoIterate(     Boolean &outGameIsOver,
  87.                                     Boolean &outThereIsANewKing,
  88.                                     Int16    inMaxSpendTimeOnce );
  89.                                     
  90.         void            SetProbabilities(     Int16    inCrossoverProb,
  91.                                             Int16    inMutationProb );
  92.         void            SetSelection(         SSelection &inSelection );
  93.         void            SetTermination(        STermination &inTermination );
  94.         void            SetSizes(            Int16    inGridSize,
  95.                                             Int16    inPopSize );
  96.         CGraphPtr        GetBestEver()         const;
  97.         
  98.         void            GetBestEverFitness( CFitness &outFitness );
  99.         
  100.         Int16            GetPopSize()         const;
  101.         Int16            GetNodeCount()         const;
  102.         Int16            GetEdgeCount()         const;
  103.         Int16            GetGridSize()         const;
  104.         Int32            GetCurGeneration()    const;
  105.         Int32            GetRunningTime()    const;
  106.         Int32            GetBestGeneration() const;
  107.         Int32            GetNoChangeGener()    const;
  108.         Int32            GetNoChangeTime()    const;
  109.         Int32            GetLeastCrossings()    const;
  110.         Int32            GetAvgCrossings()    const;
  111. };
  112.  
  113. Int16    AutomSelectionStep( Int16 inSelectionMinimum, Int16 inPopSize );
  114.  
  115.  
  116. // ===========================================================================
  117. // • Inline Functions
  118. // ===========================================================================
  119.  
  120. inline CGraphPtr
  121. CPopulation::GetBestEver()         const    
  122.                     {    return (CGraphPtr) &mBestEver;        }
  123. inline void
  124. CPopulation::GetBestEverFitness( CFitness &outFitness )    
  125.                     {    mBestEver.GetFitness( outFitness );    }
  126. inline Int16
  127. CPopulation::GetPopSize()         const    {    return mPopSize;        }
  128. inline Int16
  129. CPopulation::GetNodeCount()     const    {    return mNodeCount;        }
  130. inline Int16
  131. CPopulation::GetEdgeCount()     const    {    return mEdgeCount;        }
  132. inline Int16
  133. CPopulation::GetGridSize()         const    {    return mGridSize;        }
  134. inline Int32
  135. CPopulation::GetCurGeneration() const    {    return mCurGeneration;    }
  136. inline Int32
  137. CPopulation::GetRunningTime()    const    {    return mRunningTime;    }
  138. inline Int32
  139. CPopulation::GetBestGeneration() const    {    return mBestGeneration;    }
  140. inline Int32
  141. CPopulation::GetNoChangeGener() const    {    return mCurGeneration - 
  142.                                                    mBestGeneration;    }
  143. inline Int32
  144. CPopulation::GetNoChangeTime() const    {    return mRunningTime - 
  145.                                                    mBestTime;        }
  146. inline Int32
  147. CPopulation::GetLeastCrossings() const    {    return mLeastCrossings;    }
  148. inline Int32
  149. CPopulation::GetAvgCrossings()     const    {    return mAvgCrossings;    }
  150.